module.exports._init = function(that) {
  let now = new Date;
  let name = "date";
  let path = "_MDATS_/output/" + name;
  let offset = 1;
  let lock = 0;
  function getLinesArray(end, offset, start) {
    let struct = [];
    for (let i = start || 0; i <= end / offset; i++) {
      struct.push(i * offset);
    }
    return struct;
  }
  /** 每个月的天数 */
  const simpleDaysStruct = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  /** 每年的月 */
  const monthsStruct = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
  /** 每天的小时数字 */
  const hoursStruct = getLinesArray(23, 1);
  /** 每小时的分钟数 */
  let minutesStruct = getLinesArray(55, 5, 0);
  /**
   * 判断闰年
   */
  function isLeapYear(year) { return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0); }
  /**
   * 根据年份获得每个月的天数
   */
  function getDayStructFromYear(year) {
    let struct = simpleDaysStruct.concat([]);
    if (isLeapYear(year)) struct[1] = 29;
    struct.map(function (v, k) {
      struct[k] = getLinesArray(v, 1, 1);
    });
    return struct;
  }
  /**
   * 日期改变时间监控
   */
  that._onDateChange = function (e) {
    if (lock == 0) {
      let value = e.detail.value;
      let month = value[1];
      let day = value[2];
      let newDayColumn = getDayStructFromYear(now.getFullYear())[month];
      that._saveData('_MDATS_struct_/daysColumn', newDayColumn);
      if (day > (newDayColumn.length - 1)) {
        lock = 1;
        day = value[2] = newDayColumn.length - 1;
      }
      that._saveData('_MDATS_struct_/dateValue', value);
    } else {
      lock = 0;
    }
  }
  /**
   * 时间改变监控
   */
  that._onTimeChange = function (e) {
    let value = e.detail.value;
    that._saveData('_MDATS_struct_/timeValue', value);
  }
  /**
   * 调起方法
   */
  that._DTLauncher = function (e) {
    console.log(e);
    let dataset = e.currentTarget.dataset;
    offset = 1;
    /** Offset 获取 */
    if (typeof dataset['dtoffset'] != 'undefined') {
      offset = dataset['dtoffset'];
    }
    minutesStruct = getLinesArray(55, (offset || 5), 0);
    /** Value 获取 */
    if (typeof dataset['dtvalue'] != 'undefined') {
      try {
        now = new Date(dataset['dtvalue']);
        path = dataset['dtvalue'];
      } catch (e) {
        let date = that._getData(dataset['dtvalue']);
        now = new Date(date);
        path = dataset['dtvalue'];
      }
    }
    /** Name 获取 */
    if (typeof dataset['dtname'] != 'undefined') {
      name = dataset['dtname'];
      path = "_MDATS_/output/" + name;

    }
    /** 初始值赋值 */
    let defaultDateValue = [0, now.getMonth(), now.getDate() - 1];
    let defaultTimeValue = [now.getHours(), now.getMinutes() - now.getMinutes() % (offset||5)];
    /** 设置基本结构 */
    let struct = {
      status: undefined, // 0 - date; 1 - time
      yearsColumn: [now.getFullYear(), now.getFullYear() + 1], // Column Year 
      monthsColumn: monthsStruct, // Column Month 
      daysColumn: getDayStructFromYear(now.getFullYear())[now.getMonth()], // Column Day 
      hoursColumn: hoursStruct, // Column Year 
      minutesColumn: minutesStruct, // Column Year 
    }
    that._saveData('_MDATS_struct_', struct);
    /** 设置日期初始值 */
    that._saveData('_MDATS_struct_/dateValue', defaultDateValue);
    /** 设置时间初始值 */
    that._saveData('_MDATS_struct_/timeValue', defaultTimeValue);

    /** 唤出选择框 */
    that._saveData('_MDATS_struct_/status', 0);
  }
  /**
   * 确认方法
   */
  that._DTSubmit = function () {


    wx.showLoading({})
    that._saveData('_MDATS_struct_/status', undefined);
    function handler() {
      let date = new Date();
      let dateValue = that.data._MDATS_struct_.dateValue;
      let timeValue = that.data._MDATS_struct_.timeValue;

      date.setFullYear(dateValue[0] + now.getFullYear());
      date.setMonth(dateValue[1]);
      date.setDate(dateValue[2] + 1);

      date.setHours(timeValue[0]);
      date.setMinutes(timeValue[1] * (offset || 5));

      date = that._DTFormat(date);
      that._saveData(path, date);


      wx.hideLoading();
    }
    setTimeout(handler, 500);

  }
  /**
   * 取消方法
   */
  that._DTCancel = function () {
    that._saveData('_MDATS_struct_/status', undefined);
  }
  /**
   * result format
   */
  that._DTFormat = function(data){
    data = new Date(data);
    data = data.valueOf();
    return data;
  }
}
